<?php
namespace app\common\traits;

use think\Response;

trait ApiResponseTrait{

    final protected function responseFails( $message = '' , $code = 300 ){
        $data['code'] = $code ;
        return $this->response($message , $data);
    }

    final protected function responseSuccess( $message , $code = 200 ){
        $data['code'] = $code ;
        return $this->response($message , $data);
    }

    final private function response($message,$data) {
        $response_data = [] ;
        if( is_array($message) && array_keys($message) !== range(0, count($message) - 1) ){
           $response_data = array_merge($message,$data) ;
        }else{
            $data['data'] = $message ;
            $response_data = $data ;
        }
        return json($response_data);
    }
}
